home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / iau.arc / SELECT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-10-25  |  3.1 KB  |  126 lines

  1. /************************************************************************
  2. /    getbar()
  3. /    
  4. /    This routine displays a list of options in a line-by-line format
  5. /    and allows the user to select one of the options by pressing the
  6. /    space key (and some others optionally).  This routine has the
  7. /    same appearance of the Zenith SETUP command in ROM, and some other
  8. /    software too. The options are:
  9. /
  10. /    HOME key:    move to top selection
  11. /
  12. /    END key:    move to last selection
  13. /
  14. /    BACKSPACE key
  15. /    UP cursor
  16. /    BACKTAB keys:    move to selection above (circular)
  17. /
  18. /    SPACE
  19. /    DOWN cursor
  20. /    TAB keys:    move to selection below (circular)
  21. /
  22. /    ENTER key:    accept current selection highlighted
  23. /
  24. /    other keys:    beeps, and waits
  25. /
  26. /    INPUT from caller:
  27. /    The current cursor location will be used. When the routine
  28. /    returns, the cursor will be in the same column, but under
  29. /    the last prompt.  This routine needs to be passed a
  30. /    pointer to an array of strings, and an indication of which
  31. /    is the default.  The last+1 element must be *NULL, and no
  32. /    maxcount need be supplied.
  33. /
  34. /    OUTPUT:
  35. /    besides screen display, etc., routine returns 0-based response
  36. /    to selection (the array element selected)
  37. /
  38. /    GLOBAL VARIABLES ALTERED:
  39. /    none
  40. ***********************************************************************/
  41.  
  42. #include <stdio.h>
  43. #include <hamdefs.h>
  44. extern char *calloc();
  45.  
  46. #define SPECIAL (NORMAL|HILITE)
  47.  
  48. int getbar(choices,deflt)
  49. char *choices[];
  50. int deflt;
  51. {
  52.     int i,count,r,c,keystroke,longest,length;
  53.     int *attrib;
  54.     
  55.     for(longest=count=0;choices[count]!=NULL; ++count) {
  56.     if((length=strlen(choices[count]))>longest)
  57.         longest=length;
  58.     }
  59.  
  60.     findcsr(&r,&c);
  61.  
  62.     attrib=(int *)calloc(count,sizeof(int));
  63.  
  64.     for(i=0;i<count;i++)
  65.     attrib[i]=NORMAL;
  66.  
  67.     attrib[deflt]=SPECIAL;
  68.  
  69.     clrblk(r-1,c-1,r+count,c+longest);
  70.  
  71.     for(i=0;i<count;i++) 
  72.     atputsa(r+i,c,choices[i],attrib[i]);
  73.  
  74.     scrbox(r-1,c-1,r+count,c+longest,1,NORMAL);
  75.  
  76.     locate(r+count,c);
  77.     while((keystroke=inkeyi()) != CR) {
  78.     switch (keystroke) {
  79.         case HOME:
  80.             attrib[deflt]=NORMAL;
  81.             atputsa(r+deflt,c,choices[deflt],attrib[deflt]);
  82.             deflt=0;
  83.             attrib[deflt]=SPECIAL;
  84.             atputsa(r+deflt,c,choices[deflt],attrib[deflt]);
  85.             break;
  86.         case END:
  87.             attrib[deflt]=NORMAL;
  88.             atputsa(r+deflt,c,choices[deflt],attrib[deflt]);
  89.             deflt=count-1;
  90.             attrib[deflt]=SPECIAL;
  91.             atputsa(r+deflt,c,choices[deflt],attrib[deflt]);
  92.             break;
  93.         case BS:
  94.         case UP:
  95.         case BACKTAB:
  96.             attrib[deflt]=NORMAL;
  97.             atputsa(r+deflt,c,choices[deflt],attrib[deflt]);
  98.             if (deflt==0)
  99.                 deflt=count-1;
  100.             else
  101.                 --deflt;
  102.             attrib[deflt]=SPECIAL;
  103.             atputsa(r+deflt,c,choices[deflt],attrib[deflt]);
  104.             break;
  105.         case ' ':
  106.         case DOWN:
  107.         case '\t':
  108.             attrib[deflt]=NORMAL;
  109.             atputsa(r+deflt,c,choices[deflt],attrib[deflt]);
  110.             if (deflt==(count-1))
  111.                 deflt=0;
  112.             else
  113.                 ++deflt;
  114.             attrib[deflt]=SPECIAL;
  115.             atputsa(r+deflt,c,choices[deflt],attrib[deflt]);
  116.             break;
  117.  
  118.         default:    beep();
  119.             break;
  120.     }
  121.     locate(r+count,c);
  122.     }
  123.     free(attrib);
  124.     return deflt;
  125. }
  126.